package com.jxtti.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.PageInfo;
import com.jxtti.entity.DeptsIntroduce;
import com.jxtti.model.User;
import com.jxtti.service.DeptsIntroduceService;
import com.jxtti.utils.AuthUtil;
import com.jxtti.utils.ExceptionUtil;

@Controller
@RequestMapping("/DeptsIntroduce") 
public class DeptsIntroduceController {
	private final static Logger log= Logger.getLogger(DeptsIntroduceController.class);
	
	@Autowired //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private DeptsIntroduceService  deptsIntroduceService; 
	
	@RequestMapping(value = "/listInterface")
	@ResponseBody
	public ModelAndView listInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/DeptsInfo/DeptsInfoList.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 *  <!--科室列表  分页     -->
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/list", method = RequestMethod.POST) 
	@ResponseBody
	public Map<String, Object>  Userlist(@RequestParam Map<String, Object> data,  DeptsIntroduce deptsIntroduce, HttpServletRequest request,
			HttpServletResponse response) throws Exception{		
		Map<String, Object> result = new HashMap<String, Object>();		
		//判断是否超级管理员		
		User userInfo = AuthUtil.getUserInfo(request);
		if(userInfo.getDistrictId()==null || userInfo.getDistrictId().equals("0")){ //非超管
			data.put("fk_orgId",userInfo.getOrgId().toString()); //非超级管理员 加上权限				
		}
		
		if (data.get("fk_orgId")!=null&&!data.get("fk_orgId").equals("")) {
			String[]  orgIds=data.get("fk_orgId").toString().split(",");				
			for(int i=0;i<orgIds.length;i++ ){
				orgIds[i] = orgIds[i].replaceAll(orgIds[i],"'" + orgIds[i] + "'");			
			}			
			String cats=StringUtils.join(orgIds, ",");		
			data.put("fk_orgId",cats);	 
		}			
				
		PageInfo<DeptsIntroduce> page = deptsIntroduceService.list(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());
		return result;	
	}
	
	
	/**
	 * 查询所有数据,
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryAll")
	@ResponseBody
	public List<DeptsIntroduce> queryTCityAQIWriteAll(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		
		if (data.get("fk_orgId")!=null&&!data.get("fk_orgId").equals("")) {
			String[]  orgIds=data.get("fk_orgId").toString().split(",");				
			for(int i=0;i<orgIds.length;i++ ){
				orgIds[i] = orgIds[i].replaceAll(orgIds[i],"'" + orgIds[i] + "'");			
			}			
			String cats=StringUtils.join(orgIds, ",");		
			data.put("fk_orgId",cats);	 
		}	
		List<DeptsIntroduce>  DeptsIntroduce= deptsIntroduceService.queryAll(data);
		return DeptsIntroduce;
	}
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/DeptsInfo/DeptsInfoEdit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	@ResponseBody	
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			DeptsIntroduce  deptsIntroduce, HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {							
			int exeCount = deptsIntroduceService.add(deptsIntroduce);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}	
	

	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request,DeptsIntroduce  deptsIntroduce,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			deptsIntroduce = deptsIntroduceService.queryById(deptsIntroduce);		
			data.put("deptsIntroduce", deptsIntroduce);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/DeptsInfo/DeptsInfoEdit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
		
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			DeptsIntroduce  deptsIntroduce, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//	User user = AuthUtil.getUserInfo(request);
			//	tSite.setOperPerson(user.getUserName());
			
			int exeCount;		
			exeCount = deptsIntroduceService.update(deptsIntroduce);			
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) {
		try {			
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				DeptsIntroduce  deptsIntroduce=new DeptsIntroduce();
				deptsIntroduce.setId(Integer.parseInt(id));										
				int exeCount=deptsIntroduceService.delete(deptsIntroduce);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
}
